using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace team33_ass3
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Player player;
        Camera camera;
        World world;

        Song backgroundMusic;
        bool songStart = false;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            MediaPlayer.Volume = 0.1f;

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            backgroundMusic = Content.Load<Song>(@"sounds\bgm_tutorial");
            MediaPlayer.IsRepeating = true;

            Model tmpModel = Content.Load<Model>(@"tank\tank");
            player = new Player(tmpModel, new Vector3(0, 0, -50));

            camera = new Camera(this, graphics.GraphicsDevice.Viewport, new Vector3(0, 0, 5),
                Vector3.Zero, Vector3.Up, player);
            Components.Add(camera);

            world = new World(this, graphics, player, camera, spriteBatch);
            Components.Add(world);

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// This function shouldn't be called anywhere, unless you want to generate
        /// maps, to use for production.
        /// </summary>
        private void generateRandomMaps()
        {
            Map map = new Map(64, 64);
            String file = "level_";
            System.IO.Directory.CreateDirectory("levels");
            for (int i = 0; i < 20; i++)
            {
                map = new Map(64, 64);
                map.huntAndKill();
                map.mapTextFileOutput(@"levels\" + file + i.ToString() + ".txt");
                MapFile.writeFile(map, @"levels\" + file + i.ToString() + ".map");
            }
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            if (!songStart)
            {
                MediaPlayer.Play(backgroundMusic);
                songStart = true;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Down))
            {
                if (!MediaPlayer.Volume.Equals(0.0f))
                    MediaPlayer.Volume -= 0.1f;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                if (!MediaPlayer.Volume.Equals(1.0f))
                    MediaPlayer.Volume += 0.1f;
            }



            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);



            base.Draw(gameTime);
        }
    }
}
